package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import dao.FilesDao;
import dao.ReplysDao;
import dao.TopicsDao;
import dao.TypesDao;
import dao.UsersDao;
import dao.pojo.Types;
import dao.pojo.Users;

public class UsersDaoImpl extends BaseDaoImpl implements UsersDao {
	private TopicsDao topicsDao = null;

	private ReplysDao replysDao = null;

	private FilesDao filesDao = null;

	protected FilesDao getFilesDao() {
		return filesDao;
	}

	public void setFilesDao(FilesDao filesDao) {
		this.filesDao = filesDao;
	}

	protected ReplysDao getReplysDao() {
		return replysDao;
	}

	public void setReplysDao(ReplysDao replysDao) {
		this.replysDao = replysDao;
	}

	protected TopicsDao getTopicsDao() {
		return topicsDao;
	}

	public void setTopicsDao(TopicsDao topicsDao) {
		this.topicsDao = topicsDao;
	}

	public void addUsers(Users user) {
		Connection conn=getPool().getConnection();

		PreparedStatement pstmt = null;
		String sql = "INSERT INTO users(USERNAME,PASSWORD,TYPE_ID) VALUES(?,?,?)";
		try {
			pstmt =conn.prepareStatement(sql);
			pstmt.setString(1, user.getUserName());
			pstmt.setString(2, user.getPassword());
			pstmt.setInt(3, user.getType().getId());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			getPool().freeConnection(conn);
		}

	}

	public void deleteUsers(Users user) {
		Connection conn=getPool().getConnection();

		filesDao.deleteFilesByUser(user);
		topicsDao.deleteTopicsByUsers(user);
		replysDao.deleteAllReplysByUsers(user);

		PreparedStatement pstmt = null;
		String sql = "DELETE FROM users WHERE id=?";
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, user.getId());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			getPool().freeConnection(conn);
		}

	}

	public Users getUsersById(Users user) {
		Connection conn=getPool().getConnection();
		Users users = new Users();

		PreparedStatement pstmt = null;
		String sql = "SELECT * FROM users WHERE id=?";
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, user.getId());
			rs = pstmt.executeQuery();
			while (rs.next()) {
				users.setId(rs.getInt("id"));
				users.setUserName(rs.getString("username"));
				users.setPassword(rs.getString("password"));
				Types type = new Types();
				type.setId(rs.getInt("type_id"));
				users.setType(typesDao.getTypes(type));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			getPool().freeConnection(conn);
		}
		return users;
	}

	public Users getUsersByUserName(Users user) {
		Connection conn=getPool().getConnection();

		PreparedStatement pstmt = null;
		String sql = "SELECT * FROM users WHERE username LIKE ?";
		ResultSet rs = null;
		
		Users users = null;

		try {
			pstmt = conn.prepareStatement(sql);

			pstmt.setString(1, user.getUserName());
			rs = pstmt.executeQuery();
			while (rs.next()) {
				users=new Users();
				users.setId(rs.getInt("id"));
				users.setUserName(rs.getString("username"));
				users.setPassword(rs.getString("password"));
				Types type = new Types();
				type.setId(rs.getInt("type_id"));
				users.setType(typesDao.getTypes(type));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			getPool().freeConnection(conn);
		}
		return users;
	}

	public void saveUsers(Users user) {
		Connection conn=getPool().getConnection();

		PreparedStatement pstmt = null;
		String sql = "UPDATE users SET username=?,password=?,type_id WHERE id=?";

		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, user.getUserName());
			pstmt.setString(2, user.getPassword());
			pstmt.setInt(3, user.getType().getId());
			pstmt.setInt(4, user.getId());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			getPool().freeConnection(conn);
		}

	}

	private TypesDao typesDao;

	public TypesDao getTypesDao() {
		return typesDao;
	}

	public void setTypesDao(TypesDao typesDao) {
		this.typesDao = typesDao;
	}

	public Users getUsersById(Integer id) {
		Connection conn=getPool().getConnection();

		ResultSet rs = null;
		PreparedStatement pstmt = null;
		Users users = null;
		String sql = "SELECT * FROM users WHERE id=?";
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, id);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				users.setId(rs.getInt("id"));
				users.setUserName(rs.getString("username"));
				users.setPassword(rs.getString("password"));
				Types type = new Types();
				type.setId(rs.getInt("type_id"));
				users.setType(typesDao.getTypes(type));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			getPool().freeConnection(conn);
		}
		return users;
	}

}
